<?php
namespace Forxx;

class IDbuilder
{

    /**
     * 计算唯一字符串
     */
    public static function get_guid($namespace = '')
    {
        static $guid = '';
        $uid = uniqid("", true);
        $data = $namespace;
        $data .= $_SERVER['REMOTE_ADDR'];
        $data .= $_SERVER['REMOTE_PORT'];
        $data .= $_SERVER['HTTP_USER_AGENT'];
        $data .= $_SERVER['REQUEST_URI'];
        $data .= $_SERVER['HTTP_ACCEPT'];
        $data .= $_SERVER['REQUEST_TIME'];
        $hash = strtoupper(hash('ripemd128', $uid . $guid . md5($data)));
        $keys = self::randomkeys(4);
        return $hash.$keys;
    }

    public static function randomkeys($length)
    {
        $pattern='1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';
        $key = '';
        for($i=0;$i<$length;$i++)
        {
            $key .= $pattern{mt_rand(0,35)};    //生成php随机数
        }
        return $key;
    }

    public static function get_id()
    {
        $base = 0; //2000000000000000; //cc分段基数
        /**
         * 分段
         * 数组$n[0]为毫秒，值小于1
         * 数组$n[1]为时间戳，占10个字符
         */
        $n = explode(' ', microtime());
        return $n[1] . substr($n[0], 0, 8) * 1000000;
    }
}